Bt.LoginWindow = Ext.extend(Ext.Window,{

	//Config Options {
	 	
		//LoginWindow
		 url				: undefined
		,params				: undefined
		,redirectUrl		: undefined
		,inputLoginName		: 'usuario'
		,inputPasswordName	: 'senha'
				
		//super
		,title		: 'Autentica&#231;&#227;o'
		,baseCls	: 'x-panel'
		,cls		: 'bt-loginWindow'
		,layout		: 'form'
		,bodyStyle	: 'padding:10px;'
		,iconCls	: 'lock'
		,labelAlign	: 'right'
		,closable 	: false
		,border		: false
		,constrain	: true
		,width		: 330
		,height		: 150
		,labelWidth	: 60
				
	//}
	
	//Inits {
		
		,initComponent: function()
		{
			//config.
			Ext.apply(this,{
				 defaults:{
				  	 width			: 200
					,allowBlank		: false
					,enableKeyEvents: true
					,listeners		: {
						 scope: this
						,keyup: this._onFieldKeyUp
					}
				 }
				,items:[{
					 xtype		: 'textfield'
					,fieldLabel	: 'Usu&#225;rio'
					,name		: this.inputLoginName
				},{
					 xtype		: 'textfield'
					,fieldLabel	: 'Senha'
					,inputType	: 'password'
					,emptyText	: 'fakepass'
					,name		: this.inputPasswordName
				}]
				,buttons:[{
					 xtype		: 'bt-button'
					,text		: 'Login'
					,iconCls	: 'application-go'
					,scope		: this
					,handler	: this._onBtnLoginClick
					,listeners	: {
						 scope	: this
						,single	: true
						,render	: this._onBtnLoginRender
					}
				}]
			})
			
			//super
			Bt.LoginWindow.superclass.initComponent.call(this);
		}
		
		,initEvents: function()
		{
			//super
			Bt.LoginWindow.superclass.initEvents.call(this);
			
			this.on({
				 scope		: this
				,single		: true
				,delay		: 1000
				,afterlayout: this._onAfterLayout
			});
		}
		
	//}
	
	//Overrides {
	
		,onDestroy: function()
		{
			//super
			Bt.LoginWindow.superclass.onDestroy.apply(this,arguments);
			
			Ext.destroy(this.proxy);
			this.proxy = null;
		}
		
		,show: function()
		{
			//super
			Bt.LoginWindow.superclass.show.apply(this,arguments);
			
			//verifica erro via URL
	    	var request = window.location.href.split('?')[1];
			if(request)
			{
		        request = Ext.urlDecode(request);
				if(request['loginError'])
					this.buttons[0].showErrorTip(Bt.LoginWindow.loginErrors[request['loginError']]);
		    }
		}
	
	//}
	
	//Listeners {
		
		,_onAfterLayout: function()
		{
			//foco
			this.get(0).focus();
		}
		
		,_onBtnLoginRender: function()
		{
			
		}
		
		,_onFieldKeyUp: function(field, e)
		{
			if(e.getKey() === e.ENTER )
			{
				this._onBtnLoginClick();
				e.stopEvent();
			}
		}
		
		,_onBtnLoginClick: function()
		{
			//validate and value
			var isValid = true;
			var values = {};
			
			this.items.each(function(item)
			{
				if(!item.isValid())
					isValid = false;
					
				if(isValid && item.name)
					values[item.name] = item.getValue();
			})
			
			if(!isValid)
				return;
			
			this.buttons[0].setLoading(true);
			
			//request
			Ext.Ajax.request({
				 url	: this.url 
				,params	: Ext.apply(this.params||{},values)
				,scope	: this
				,failure: function()
				{
					this.buttons[0].showErrorTip('N&#227;o foi poss&#237;vel contactar o servidor')	
				}
				,callback: function()
				{
					this.buttons[0].setLoading(false);
				}
				,success: function(response)
				{
					try{
					
						//converte resposta
						response = Ext.decode(response.responseText);
						
						//valida resposta
						if( response.success === true )
						{
							this.disable();
							window.location.href = this.redirectUrl;
						}
						else
						{
							this.buttons[0].showErrorTip(response.message||"Usu&#225;rio e/ou senha inv&#225;lidos");	
						}
					}
					catch(e)
					{
						//json inv�lido
						this.buttons[0].showErrorTip('Erro ao receber resposta do servidor');	
						return;
					}
				}
			});	
		}
	
	//}
	
	
});


Bt.LoginWindow.loginErrors = {
	1: 'A sess&#227;o expirou, refa&#231;a o login'
}
